package utility;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public final class JdbcUtils {

	private static final String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
	private static final String user="root";
	private static final String password="root";
	
	
	static {
		String className="com.mysql.jdbc.Driver";
		try {
			Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ExceptionInInitializerError(e);
		}
	}
	private JdbcUtils(){
	}
	
	static public Connection getConnection() throws SQLException{
		return DriverManager.getConnection(url, user, password);
	}
	
	static public void free(ResultSet rs,Statement st,Connection con){
		try {
			if(rs!=null){
				rs.close();
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(st!=null){
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(con!=null){
						try {
							con.close();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
}
